#include <stdio.h>

int maxsum(int arr[], int lo, int ri)
{
    int total = 0;
    int sum = 0;

    //判断是否全部为负数
    sum = arr[lo];

    for (int i = lo + 1; i <= ri; ++i) {
        if (sum < arr[i]) {
            sum = arr[i];
        }
    }

    if (sum < 0) {
        return sum;
    }

    //计算有正有负
    for (int i = lo; i <= ri; ++i) {
        sum += arr[i];

        if (sum <= 0) {
            sum = 0;
        }

        if (sum > total) {
            total = sum;
        }
    }

    return total;
}

#define SIZE(arr) (sizeof(arr)/sizeof(arr[0]))

int main(int argc, const char *argv[])
{
    int arr[] = {1, -2, 3, 10, -4, 7, 2, -5};

    for (int i = 0; i < SIZE(arr); ++i) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    printf("the total is %d", maxsum(arr, 0, SIZE(arr) - 1));
    return 0;
}
